//
//  SEInspListResultView.swift
//  SBSafetyApp
//
//  Created by Lina on 2022/12/11.
//

import Foundation
import UIKit

class InspUploadParam: Encodable {
    var tmplId: Int64?
    var tmplName: String?
    var recordId: Int64?
    var status: String?
    var remark: String?
    var summary: String?
    var images: [[String: String]]?
    
    init(tmplId: Int64?, tmplName: String?, recordId: Int64?, status: String?, remark: String?, summary: String?, images: [[String : String]]? = nil) {
        self.tmplId = tmplId
        self.tmplName = tmplName
        self.recordId = recordId
        self.status = status
        self.remark = remark
        self.summary = summary
        self.images = images
    }
    
    init(item: InspResultParam, images: [[String : String]]? = nil) {
        self.tmplId = item.tmplId
        self.tmplName = item.tmplName
        self.recordId = item.recordId
        self.status = item.status
        self.remark = item.remark
        self.summary = item.summary
        self.images = images

    }
}

class InspResultParam {
    var tmplId: Int64?
    var tmplName: String?
    var recordId: Int64?
    var status: String?
    var remark: String?
    var summary: String?
    var images: [UIImage]?
    
    init(tmplId: Int64?, tmplName: String?, recordId: Int64?, status: String?, remark: String?, summary: String?, images: [UIImage]? = nil) {
        self.tmplId = tmplId
        self.tmplName = tmplName
        self.recordId = recordId
        self.status = status
        self.remark = remark
        self.summary = summary
        self.images = images
    }
}

class SEInspListResultView: UIScrollView {
    let submitBtn = UIButton.createPrimaryLarge("提交")

    let titleBV = UIView.createBase()
    let nameL = UILabel()
    let desL = UILabel()

    // 巡检结果
    let basicBV = UIView.createBase()
    let xjztItem = LabelPickerEffect(withPlaceholder: "巡检状态")   // 巡检状态
    let line = UIView()

    let ycBV = UIView()
    let ycqksmItem = TextViewEditEffect(withPlaceholder: "异常情况说明")  // 异常情况说明
    
    var recordParam: InspResultParam!

    override init(frame: CGRect) {
        super.init(frame: frame)
        keyboardDismissMode = .interactive
        showsVerticalScrollIndicator = false
        setupUI()
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    func updateUI(withModal modal: InspResultParam?) {
        nameL.text = modal?.tmplName
        desL.text = modal?.remark
        ycqksmItem.value = modal?.summary
        recordParam = modal
        imgPickerView.images = modal?.images ?? []

        if recordParam.status == "2" {
            recordParam.status = "0"
        }
        updateStatusUI()
    }
    
    func updateStatusUI() {
        if recordParam.status == "0" {
            xjztItem.value = "正常"
            ycBV.layer.opacity = 0.0
            ycBV.snp.remakeConstraints { make in
                make.left.equalTo(line.snp.left)
                make.right.equalTo(line.snp.right)
                make.top.equalTo(xjztItem.snp.bottom).offset(10)
                make.bottom.equalToSuperview()
                make.height.equalTo(0)
            }
        } else if recordParam.status == "1" {
            xjztItem.value = "异常"
            ycBV.layer.opacity = 1.0
            ycBV.snp.remakeConstraints { make in
                make.left.equalTo(line.snp.left)
                make.right.equalTo(line.snp.right)
                make.top.equalTo(xjztItem.snp.bottom).offset(10)
                make.bottom.equalToSuperview().offset(-14)
            }
        }
    }
    
    // MARK: - Setup
    func setupTitleView() {
        addSubview(titleBV)
        titleBV.snp.makeConstraints { make in
            make.top.equalTo(self.snp.top).offset(10)
            make.centerX.equalToSuperview()
            make.width.equalTo(ScreenWidth - 20)
        }
        
        nameL.font = .systemFont(ofSize: 17)
        titleBV.addSubview(nameL)
        nameL.snp.makeConstraints { make in
            make.top.equalTo(titleBV.snp.top).offset(18)
            make.centerX.equalToSuperview()
        }
        
        desL.textColor = .hex("#306EC8")
        desL.font = .systemFont(ofSize: 16)
        desL.numberOfLines = 0
        desL.textAlignment = .center
        titleBV.addSubview(desL)
        desL.snp.makeConstraints { make in
            make.top.equalTo(nameL.snp.bottom).offset(8)
            make.left.equalToSuperview().offset(20)
            make.right.equalToSuperview().offset(-20)
            make.centerX.equalToSuperview()
            make.bottom.equalToSuperview().offset(-16)
        }
    }
    
    func setupBasicView() {
        addSubview(basicBV)
        basicBV.snp.makeConstraints { make in
            make.top.equalTo(titleBV.snp.bottom).offset(10)
            make.centerX.equalToSuperview()
            make.width.equalTo(ScreenWidth - 20)
        }
        
        let basicTV = TitleItemView(withTitle: "巡检结果", hasIcon: false)
        basicBV.addSubview(basicTV)
        basicTV.snp.makeConstraints { make in
            make.top.left.right.equalToSuperview()
            make.height.equalTo(46)
        }
        
        line.backgroundColor = .hex("#ECECEC")
        basicBV.addSubview(line)
        line.snp.makeConstraints { (make) -> Void in
            make.top.equalTo(basicTV.snp.bottom)
            make.left.equalToSuperview().offset(13)
            make.right.equalToSuperview().offset(-13)
            make.height.equalTo(0.5)
        }
        
        xjztItem.dataSource = [
            LabelDictModal(type: "991", dictLabel: "正常", dictValue: "0"),
            LabelDictModal(type: "992", dictLabel: "异常", dictValue: "1")
        ]
        xjztItem.value = ""
        xjztItem.didSelectItemWith = { (modal) -> () in
            self.recordParam.status = modal.dictValue!
            self.updateStatusUI()
        }
        basicBV.addSubview(xjztItem)
        xjztItem.snp.makeConstraints { make in
            make.left.equalTo(line.snp.left)
            make.right.equalTo(line.snp.right)
            make.top.equalTo(line.snp.bottom).offset(15)
            make.height.equalTo(45)
        }
        
        basicBV.addSubview(ycBV)
        ycBV.snp.makeConstraints { make in
            make.left.equalTo(line.snp.left)
            make.right.equalTo(line.snp.right)
            make.top.equalTo(xjztItem.snp.bottom).offset(10)
            make.bottom.equalToSuperview()
            make.height.equalTo(0)
        }
        
        ycqksmItem.backgroundColor = .bg
        ycBV.addSubview(ycqksmItem)
        ycqksmItem.snp.makeConstraints { make in
            make.top.left.right.equalToSuperview()
            make.height.equalTo(120)
        }
        
        ycBV.addSubview(imgPickerView)
        imgPickerView.snp.makeConstraints { make in
            make.left.right.bottom.equalToSuperview()
            make.top.equalTo(ycqksmItem.snp.bottom).offset(10)
        }
    }
    
    func setupUI() {
        setupTitleView()
        
        // 巡检结果
        setupBasicView()
        
        // 提交按钮
        addSubview(submitBtn)
        submitBtn.snp.makeConstraints { make in
            make.top.equalTo(basicBV.snp.bottom).offset(48)
            make.centerX.equalToSuperview()
            make.width.equalTo(ScreenWidth - 20)
            make.height.equalTo(50)
            make.bottom.equalToSuperview().offset(-15)
        }
    }
    
    lazy var imgPickerView: BSImagePickerView = {
        let picker = BSImagePickerView()
        picker.key = "设备照片"
        picker.maxCount = 6
        picker.rowCount = 4
        picker.itemHeight = 70
        return picker
    }()
}
